<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('字段属性配置列表')" />
	<style type="text/css">
		.layui-btn-container .layui-btn{margin:10px}
	</style>
</head>
<body>
<form>
	<div class="layuiadmin-header-box">
		<th:block th:include="listinclude :: searchTop" />
	</div>
</form>
<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
<div class="layui-btn-container" style="float: right;margin-right: 20px;">
	<input class="layui-btn layui-table-tools" type="button" lay-submit="" lay-filter="btn_save" value="保存">
	<input class="layui-btn layui-btn-normal layui-table-tools" type="button" lay-submit="" lay-filter="btn_save_close" value="保存并关闭">
	<input class="layui-btn layui-btn-primary layui-table-tools" type="button" lay-submit="" lay-filter="btn_close" value="关闭">
</div>
<script type="text/html" id="tpl_fieldFrontFixed">
    <input type="checkbox" lay-skin="switch" lay-text="固定|活动" tIndex='{{d.LAY_TABLE_INDEX}}' name='fieldFrontFixed' lay-filter='change' {{ d.fieldFrontFixed? 'checked' : '' }}>
</script>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "system/modulefieldset",
		moduleTable=[[${moduleTable}]];
	/*]]>*/
</script>
<script type="text/javascript">
var $,layer,form,table,wherestr={},dataStr=[];
layui.use(['table','form','layer','jquery'], function () {
     $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table;
     $.tablelist.init({
         elem: '#dhtable'
	     ,modalName: "字段属性设置"
         ,url: prefix + "/tablelist?moduleTable="+window.location.href.split("=")[1]
     	 ,page:false
     	 ,limit:100
         ,cols: [[
        	 {type:'numbers'},
 	        {field: 'fieldAlias',title: '列名',sort:true,edit:'text',width:200},
 	        {field: 'fieldFrontEdittext',title: '可编辑',sort:true,width:100,align:"center"},
 	        {field: 'fieldFrontRequiredtext',title: '必填',sort:true,width:100,align:"center"},
 	       	{field: 'fieldFrontFixed',title: '是否固定列',sort:true,width:130,align:"center",templet:'#tpl_fieldFrontFixed'},
 	        {field: 'fieldFrontOrder',title: '排列顺序',sort:true,width:110,edit:'text'},
 	      	{field: 'fieldFrontWidth',title: '列宽度',sort:true,edit:'text'}
 	    ]
     	]
        ,height:'full-120'
     });
     //监听单元格编辑
     table.on('edit(ftable)', function(obj){
   	 	// console.log(obj);
 		var selector = obj.tr.selector+' td[data-field="'+obj.field+'"] div';//获取当前行dom对象
 		var oldtext = $(selector).text(),//修改前的数据
 			editField = obj.field,//修改的字段
 			editRow = obj.data,//该行数据
 			editValue=obj.value,//修改后的数据
 			dataindex = $(obj.tr.selector).attr('data-index');
//  		obj.update({fieldFrontOrder:'5'});
		if(editField=='fieldFrontOrder'){
	 		if(eachOrder(editValue,dataindex)){
	 			layer.msg('出现重复，已重新排序！',{icon:7});
	 			table.reload('dhtable',{data:table.cache.dhtable});
	 		}
		}
 	});
   	//监听开关动作
 	form.on('switch(change)', function(obj){
		var cName=obj.elem.name,cChecked=obj.elem.checked,cIndex=$(this).attr('tindex');
		table.cache.dhtable[cIndex][cName]=cChecked;
    });
	//监听排序事件 
    table.on('sort(ftable)', function(obj){
    	$.tablelist.sort('dhtable',obj);
    });
  	//搜索按钮
  	$('#btn-search').on('click', function () {
  		$.each(wherestr,function(i,v){$("#"+i).val()?wherestr[i]=$("#"+i).val():wherestr[i]='';});//动态获取条件的值
         table.reload('dhtable', {page: {curr: 1},where:wherestr});
    });
  	//监听下拉框——添加条件
  	form.on('select(fft)', function(obj){
  		addMore(this);
  	});
    //保存
    form.on('submit(btn_save)', function(data){
    	$.operate.post(prefix + "/editpost", {'moduleFieldSetItems':JSON.stringify(table.cache.dhtable, null, 2)})
   	}); 
    //保存并关闭
    form.on('submit(btn_save_close)', function(data){
   	 $.operate.post(prefix + "/editpost", {'moduleFieldSetItems':JSON.stringify(table.cache.dhtable, null, 2)},setTimeout(function(){top.layui.admin.closeThisTabs()}, 2000));
 	}); 
	//关闭
    form.on('submit(btn_close)', function(data){
    	top.layui.admin.closeThisTabs();
 	}); 
 });
 function eachOrder(d,j){//递归修改排序字段
	 var value='',index='',flag=false;
	 $.each(table.cache.dhtable,(i,v)=>{
		 if(v.fieldFrontOrder==d && v.LAY_TABLE_INDEX!=j){
			 v.fieldFrontOrder++;
			 value=v.fieldFrontOrder;
			 index=v.LAY_TABLE_INDEX;
			 flag=true;
		 }
	 })
	if(!!value)eachOrder(value,index);
	return flag
 }
 </script>
</body>
</html>